const app = getApp()
import { getUserCollection, deleteUserCollection, addUserCollection, getCollectionCases } from '../../../api/index'

Page({
  data: {
    safeBottom: 0,
    tabActive: 'material',
    folderList: [],
    currentItem: null,
    showSheet: false,
    actions: [
      { name: '修改收藏夹', value: 'change' },
      { name: '删除收藏夹', value: 'delete' }
    ],
    showInfoPop: false,
    caseList: [],
    isEmpty: false,
    currentPage: 1,
    pageSize: 15,
    refresher: false
  },
  async onRefresh () {
    await this.getCaseList()
    this.setData({ refresher: false })
  },
  seeDetail ({ currentTarget: { dataset: { item } } }) {
    wx.$nav.navigateTo(`/pages/account/collectionMaterial/index?name=${item.collectionName}&id=${item.id}`)
  },
  cancel () {
    this.setData({ currentItem: null, showSheet: false })
  },
  handleSelect ({ detail }) {
    if (detail.value === 'delete') {
      this.deleteFolder()
    } else {
      this.setData({ showInfoPop: true, showSheet: false })
    }
  },
  closeInfoPop () {
    this.setData({ currentItem: null, showInfoPop: false })
  },
  changeInfo ({ detail }) {
    this.changeFolder(detail)
  },
  createFolder () {
    this.setData({ showInfoPop: true })
  },
  async changeFolder (form) {
    try {
      await addUserCollection({ ...form })
      if (form.id) {
        wx.showToast({ title: '修改成功！', icon: 'none' })
      } else {
        wx.showToast({ title: '创建成功！', icon: 'none' })
      }
      this.setData({ currentItem: null, showInfoPop: false })
      this.getFolderList()
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  changeCase ({ detail }) {
    let index = this.data.caseList.findIndex(row => row.materialCaseId === detail.materialCaseId)
    if (index > -1) {
      let changeKey = `caseList[${index}]`
      this.setData({
        [changeKey]: detail
      })
    }
  },
  async deleteFolder () {
    try {
      const { confirm } = await wx.$pro.showModal({ title: '删除提示', content: '是否删除该文件夹？删除后收藏数据无法恢复。', showCancel: true, cancelText: '取消' })
      if (confirm) {
        await deleteUserCollection({ id: this.data.currentItem.id })
        wx.showToast({ title: '删除成功！', icon: 'none' })
        this.setData({ currentItem: null, showSheet: false })
        this.getFolderList()
      }
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  edit ({ currentTarget: { dataset: { item } } }) {
    this.setData({ currentItem: item, showSheet: true })
  },
  changeTab ({ currentTarget: { dataset: { value } } }) {
    this.setData({ tabActive: value })
    if (value === 'case') this.getCaseList()
    else this.getFolderList()
  },
  async getFolderList () {
    try {
      const { ResultSet } = await getUserCollection()
      this.setData({
        folderList: ResultSet.userData.userCollections
      })
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async loadMore () {
    try {
      if (this.loading || this.alloaded) return
      this.loading = true
      const { ResultSet } = await getCollectionCases({ currentPage: this.data.currentPage, pageSize: this.data.pageSize })
      this.alloaded = !ResultSet.userData.collectionCases.length
      this.data.isEmpty = !ResultSet.userData.collectionCases.length
      this.setData({ isEmpty: this.data.isEmpty })
      if (ResultSet.userData.collectionCases.length) {
        this.data.currentPage = this.data.currentPage + 1
        this.data.caseList.push(...ResultSet.userData.collectionCases)
        this.setData({
          caseList: this.data.caseList,
          currentPage: this.data.currentPage
        })
      }
      this.loading = false
    } catch (err) {
      console.log(err)
      this.loading = false
      wx.showToast({ title: err.message || err.ResultSet.errors })
    }
  },
  async getCaseList () {
    try {
      const { ResultSet } = await getCollectionCases({ currentPage: 1, pageLines: this.data.pageSize })
      if (ResultSet.userData.collectionCases.length) this.data.currentPage = 2
      this.data.isEmpty = ResultSet.userData.collectionCases.length && ResultSet.userData.collectionCases.length < this.data.pageSize
      this.setData({
        caseList: ResultSet.userData.collectionCases,
        currentPage: this.data.currentPage,
        isEmpty: this.data.isEmpty
      })
      // this.setData({
      //   caseList: ResultSet.userData.collectionCases
      // })
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  onLoad () {
    const { screenHeight, safeArea: { bottom } } = wx.$pro.getSystemInfoSync()
    this.setData({ safeBottom: screenHeight - bottom })
    this.getFolderList()
  }
})